#include "myapplication.h"
#include <mmsystem.h>
#include "LoadLua.h"

myApplication * myApplication::s_pInstance = NULL;

// you can use constructor to initialise variables
myApplication::myApplication()
: theCamera(NULL)
, theRTL(NULL)
, theLevels(NULL)
, theGameMenu(NULL)
, mouseInfo(NULL)
, track_loading( 0 )
, track_loading_done( false )
{
}

myApplication::~myApplication()
{
	if (theCamera != NULL)
	{
		delete theCamera;
		theCamera = NULL;
	}

	if(theLevels != NULL){
		delete theLevels;
		theLevels = NULL;
	}

	if(theRTL != NULL){
		delete theRTL;
		theRTL = NULL;
	}

	if(theGameMenu != NULL)
	{
		delete theGameMenu;
		theGameMenu = NULL;
	}

	if(mouseInfo != NULL){
		delete mouseInfo;
		mouseInfo = NULL;
	}

	if(theSound != NULL){
		delete theSound;
		theSound = NULL;
	}

	our_font.clean();
}

myApplication* myApplication::getInstance()
{
	if(s_pInstance == NULL)
	{
		s_pInstance = new myApplication();
	}

	return s_pInstance;
}

// Drop Instance
void myApplication::Drop()
{
	if ( s_pInstance != NULL )
	{
		delete s_pInstance;
		s_pInstance = NULL;
	}
}

void myApplication::Update(void)
{
	static bool toggleUpButton = false;
	if(myKeys[ GLUT_KEY_UP ] == true)
	{
		if (!toggleUpButton)
		{
			if(theGameMenu->MenuType == CGameMenu::MAIN_MENU)
			{
				if (theGameMenu->BGM == true)
				{
					theSound->Play(Sound::MENUKEY);
				}
			}

			toggleUpButton = true;
			theGameMenu->ButtonUp();
			//myKeys[ GLUT_KEY_UP ] = false;
		}
	}
	else toggleUpButton = false;

	static bool toggleDownButton = false;
	if(myKeys[ GLUT_KEY_DOWN ] == true)
	{
		if (!toggleDownButton)
		{
			if(theGameMenu->MenuType == CGameMenu::MAIN_MENU)
			{
				if (theGameMenu->BGM == true)
				{
					theSound->Play(Sound::MENUKEY);
				}
			}
			toggleDownButton = true;
			theGameMenu->ButtonDown();
			//myKeys[ GLUT_KEY_DOWN ] = false;
		}
	}
	else toggleDownButton = false;
	
	static bool toggleLeftButton = false;
	if(myKeys[ GLUT_KEY_LEFT ] == true)
	{
		if (!toggleLeftButton)
		{
			if(theGameMenu->MenuType == CGameMenu::CHARACTER_MENU ||
				theGameMenu->MenuType == CGameMenu::LOBBY_MENU)
			{
				if (theGameMenu->BGM == true)
				{
					theSound->Play(Sound::MENUKEY);
				}
			}

			toggleLeftButton = true;
			theGameMenu->ButtonLeft();
			//myKeys[ GLUT_KEY_LEFT ] = false;
		}
	}
	else toggleLeftButton = false;
	
	static bool toggleRightButton = false;
	if(myKeys[ GLUT_KEY_RIGHT ] == true)
	{
		if (!toggleRightButton)
		{
			if(theGameMenu->MenuType == CGameMenu::CHARACTER_MENU ||
			theGameMenu->MenuType == CGameMenu::LOBBY_MENU)
			{
				if (theGameMenu->BGM == true)
				{
					theSound->Play(Sound::MENUKEY);
				}
			}

			toggleRightButton = true;
			theGameMenu->ButtonRight();

			//myKeys[ GLUT_KEY_RIGHT ] = false;
		}
	}
	else toggleRightButton = false;
	
	 static bool toggleQ = false;
 if(myKeys['q'])
 {
	 if (!toggleQ)
	 {
		 if(theGameMenu->MenuType == CGameMenu::LOBBY_MENU)
		 {
			 if (theGameMenu->BGM == true)
			 {
				 theSound->Play(Sound::SELECT);
			 }

			 int temp = -1;

			 if  ( theGameMenu->LobbyButtonType == CGameMenu::SWORD_AND_SHIELD_SHOP_BUTTON ) temp = CWeapon::SWORDANDSHIELD;
			 else if ( theGameMenu->LobbyButtonType == CGameMenu::GREAT_SWORD_SHOP_BUTTON  ) temp = CWeapon::LONGSWORD;
			 else if ( theGameMenu->LobbyButtonType == CGameMenu::DUAL_SWORD_SHOP_BUTTON   ) temp = CWeapon::DUALSWORD;

			 if ( !theLevels->WeaponSelect( static_cast< CWeapon::WEAPON >( temp ) ) ) return;
		 }

		 theGameMenu->CheckKeys();

		 toggleQ = true;
	 }
 }
 else toggleQ = false;

 static bool toggleEsc = false;

 if(myKeys[ 27 ] == true)
	{
		if (!toggleEsc)
		{
			toggleEsc = true;

			if (theGameMenu->MenuType == CGameMenu::MAIN_MENU)
			{
				glutLeaveMainLoop();
			}
			else if ( theGameMenu->MenuType == CGameMenu::MISSION_MENU )
			{
				theGameMenu->MenuType = CGameMenu::LOBBY_MENU;
				theLevels->EndLevel();
			}
			else
			{
				theLevels->SavePlayer();

				theGameMenu->MenuType = CGameMenu::MAIN_MENU;
				theGameMenu->CharacterButtonType = 0;
				theGameMenu->MainMenuButtonType = 0;
				theGameMenu->CharacterButtonType = 0;
				theGameMenu->SettingButtonType = 0;
				theGameMenu->LobbyButtonType = 0;
				theGameMenu->MissionDifficultyButtonType = 0;
				theGameMenu->MissionButtonType = 0;
			}
		}
	}
	else toggleEsc = false;

	static bool toggleEnter = false;
	if(myKeys[VK_RETURN ] == true)
	{
		if (!toggleEnter)
		{
			toggleEnter = true;

			if(theGameMenu->MenuType == CGameMenu::SETTING_MENU)
			{
				if (theGameMenu->BGM == false)
				{
					theSound->Play(Sound::BACKGROUND,true);
				}
				else 
				{
					theSound->StopAll();
				}
			}
			else if ( theGameMenu->MenuType == CGameMenu::CHARACTER_MENU )
			{
				if		( theGameMenu->CharacterButtonType == CGameMenu::SWORD_AND_SHIELD_BUTTON )	theLevels->CreateNewPlayer( CWeapon::SWORDANDSHIELD );
				else if	( theGameMenu->CharacterButtonType == CGameMenu::GREAT_SWORD_BUTTON )		theLevels->CreateNewPlayer( CWeapon::LONGSWORD );
				else if	( theGameMenu->CharacterButtonType == CGameMenu::DUAL_SWORD_BUTTON )		theLevels->CreateNewPlayer( CWeapon::DUALSWORD );

				theLevels->SavePlayer();
			}
			else if ( theGameMenu->MenuType == CGameMenu::MAIN_MENU )
			{
				if ( theGameMenu->MainMenuButtonType == CGameMenu::LOAD_BUTTON )
				{
					theLevels->LoadPlayer();
				}
			}
			else if(theGameMenu->MenuType == CGameMenu::LOBBY_MENU)
			{
				int temp_wep	= -1,
					temp_item	= -1,
					temp_req	= -1;

				// which button?
				if (theGameMenu->LobbyButtonType == CGameMenu::SWORD_AND_SHIELD_SHOP_BUTTON)
				{
					temp_wep	= static_cast<int>( CWeapon::SWORDANDSHIELD );
					temp_item	= static_cast<int>( MATREQ_ITEM_SNS );
					temp_req	= MATREQ_SNS;
				}
				else if	(theGameMenu->LobbyButtonType == CGameMenu::GREAT_SWORD_SHOP_BUTTON)
				{
					temp_wep	= static_cast<int>( CWeapon::LONGSWORD );
					temp_item	= static_cast<int>( MATREQ_ITEM_GS );
					temp_req	= MATREQ_GS;
				}
				else if (theGameMenu->LobbyButtonType == CGameMenu::DUAL_SWORD_SHOP_BUTTON)
				{
					temp_wep	= static_cast<int>( CWeapon::DUALSWORD );
					temp_item	= static_cast<int>( MATREQ_ITEM_DS );
					temp_req	= MATREQ_DS;
				}
				else if (theGameMenu->LobbyButtonType == CGameMenu::POTION_BUTTON)
				{
					temp_item	= static_cast<int>( MATREQ_ITEM_POT ); 
					temp_req	= MATREQ_POT;
				}

				// not the right thing, end
				if ( temp_wep == -1 && temp_item == -1 ) return;

				// check for enough materials
				if ( theLevels->GetInventory()->CheckQuantity( static_cast<Item::no>( temp_item ) ) >= temp_req )
				{
					// potions?
					if ( theGameMenu->LobbyButtonType == CGameMenu::POTION_BUTTON )
					{
						theLevels->GetInventory()->AddItem( Item::POTIONS );
					}
					// unlocked yet?
					else if ( !theLevels->CheckWeaponUnlock( static_cast< CWeapon::WEAPON >( temp_wep ) ) )
					{
						// if not, unlock
						theLevels->WeaponUnlock( static_cast< CWeapon::WEAPON >( temp_wep ) );
					}
					// unlocked already
					else
					{
						// level up
						if ( theLevels->CheckWeaponLevel( static_cast< CWeapon::WEAPON >( temp_wep ) ) < MAX_WEP_LEVEL )
							theLevels->WeaponLevel( static_cast< CWeapon::WEAPON >( temp_wep ), 1 );
					}

					// take away items used
					theLevels->GetInventory()->RemoveItem( static_cast<Item::no>( temp_item ), temp_req );

					// rearrange items
					theLevels->SavePlayer();
					theLevels->LoadPlayer();
				}
			}

			theGameMenu->CheckEnterButton();
		}
	}
	else toggleEnter = false;

/*	if (theGameMenu->MenuType == CGameMenu::MAIN_MENU)
	{
		theLevels->UpdateLevel(myKeys, theRTL->GetFrameDeltaTime() );
		theCamera->SetPosition(theLevels->ReturnCharPos().x,2.0,-5);
	}
*/
	if (theGameMenu->MenuType == CGameMenu::MISSION_MENU)
	{
		theLevels->UpdateLevel(myKeys, theRTL->GetFrameDeltaTime());
		theCamera->SetPosition(theLevels->ReturnCharPos().x,2.0,-5);
	}

	//if(myKeys['1']){
	//	theLevels->SwitchLevel(1);
	//}else if(myKeys['2']){
	//	theLevels->SwitchLevel(2);
	//}else if(myKeys['3']){
	//	theLevels->SwitchLevel(3);
	//}else if(myKeys['4']){
	//	theLevels->SwitchLevel(4);
	//}

	theLevels->InitLevel();
}

void myApplication::renderScene(void) 
{
	// Clear the buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	theCamera->Update();

	// Preparing Real-Time Loop
	theRTL->UpdatePre();

	// Real-Time Loop
	while ( theRTL->UpdateCheck() )
	{
		// Update Real-Time Loop
		theRTL->UpdateLoop();

		// Calculate the framerate
		calculateFPS();

		// load if have yet to do so
		if ( LoadAll() )
		{
			// Update
			this->Update();
		}
	}

	// If Playing Solo And Game Logic Takes Way Too Long, Discard Pending Time
	theRTL->UpdatePlayingSoloDiscardPendingTime();

	// Enable 2D text display and HUD
	theCamera->SetHUD( true );

	// Loading
	if ( !track_loading_done )
	{
		glEnable(GL_TEXTURE_2D);	//Enable texture
		glEnable(GL_BLEND);			//Enable blend
		glBlendFunc( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );

		glPushMatrix ();
			glColor3f(1.0f, 1.0f, 1.0f);
			glBindTexture(GL_TEXTURE_2D, splash.texID);
			glBegin( GL_QUADS );
				glTexCoord2f(0,1); glVertex2f(-0, -0);		//Bottom left
				glTexCoord2f(0,0); glVertex2f(-0, 600);		//Top left
				glTexCoord2f(1,0); glVertex2f(800, 600);	//Top right
				glTexCoord2f(1,1); glVertex2f(800, -0);		//Bottom right
			glEnd();
		glPopMatrix ();

		//Loading...
		glPushMatrix ();
			glColor3f(1.0f, 1.0f, 1.0f);
			glBindTexture(GL_TEXTURE_2D, loading.texID);
			glBegin( GL_QUADS );
				glTexCoord2f(0,1); glVertex2f(600, 500);	//Bottom left
				glTexCoord2f(0,0); glVertex2f(600, 600);	//Top left
				glTexCoord2f(1,0); glVertex2f(800, 600);	//Top right
				glTexCoord2f(1,1); glVertex2f(800, 500);	//Bottom right
			glEnd();
		glPopMatrix ();

		glDisable(GL_BLEND);		//Enable blend
		glDisable(GL_TEXTURE_2D);	//Disable texture
	}
	// Done Loading
	else
	{
		theGameMenu->RenderMenus( theRTL->GetFrameDeltaTime() );

		theLevels->RenderLevel();

		if(theGameMenu->MenuType == CGameMenu::LOBBY_MENU)
		{
			glColor3f(1.0f, 0.0f, 0.0f);
			freetype::print(our_font, 120, 400, "MATERIALS REQUIRED");
				
			glPushMatrix();
				glScalef(0.5f, 0.5f, 1.0f);
				glColor3f(1.0f, 0.0f, 0.0f);
				freetype::print(our_font, 560, 420, "ITEM DESCRIPTION");
			glPopMatrix();

			if(theGameMenu->LobbyButtonType == CGameMenu::SWORD_AND_SHIELD_SHOP_BUTTON)
			{
				glPushMatrix();
					glScalef(3.0f, 3.0f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 330, 220, "%d/%d", theLevels->GetInventory()->CheckQuantity( MATREQ_ITEM_SNS ), MATREQ_SNS );
				glPopMatrix();
				
				glPushMatrix();
				glTranslatef(0.0f, 0.0f, 0.0f);

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(1.0f, 1.0f, 0.0f);
					freetype::print(our_font, 560, 390, "Sword and Shield" );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 560, 360, "Damage: \nAttack speed: \nWeapon Level: " );
				glPopMatrix();
	
				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 1.0f);
					freetype::print(our_font, 640, 360, "Normal" );
				glPopMatrix();
	
				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 1.0f);
					freetype::print(our_font, 680, 325, "Normal" );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 1.0f);
					freetype::print(our_font, 680, 290, "%d/%d", theLevels->GetPlayer()->Check_Weapon_Level(CWeapon::WEAPON::SWORDANDSHIELD),MAX_WEP_LEVEL);
				glPopMatrix();

				glPushMatrix();
					glScalef(0.9f, 0.9f, 1.0f);
					
					if ( theLevels->CheckWeaponUnlock( CWeapon::SWORDANDSHIELD ) )
					{
						glColor3f( 0.0f, 1.0f, 0.0f );
						freetype::print( our_font, 568, 230, "UNLOCKED" );
						theGameMenu->CheckWeapon = true;
					}
					else
					{
						glColor3f( 1.0f, 0.0f, 0.0f );
						freetype::print( our_font, 590, 230, "LOCKED" );
						theGameMenu->CheckWeapon = false;
					}
				glPopMatrix();

				glPopMatrix();
			}
			else if(theGameMenu->LobbyButtonType == CGameMenu::GREAT_SWORD_SHOP_BUTTON)
			{
				glPushMatrix();
					glScalef(3.0f, 3.0f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 330, 220, "%d/%d", theLevels->GetInventory()->CheckQuantity( MATREQ_ITEM_GS ), MATREQ_GS );
				glPopMatrix();

				glPushMatrix();
				glTranslatef(0.0f, 0.0f, 0.0f);

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(1.0f, 1.0f, 0.0f);
					freetype::print(our_font, 560, 390, "Great Sword" );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 560, 360, "Damage: \nAttack speed: \nWeapon Level: " );
				glPopMatrix();
	
				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 0.0f);
					freetype::print(our_font, 640, 360, "High" );
				glPopMatrix();
	
				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(1.0f, 0.0f, 0.0f);
					freetype::print(our_font, 680, 325, "Slow " );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 1.0f);
					freetype::print(our_font, 680, 290, "%d/%d", theLevels->GetPlayer()->Check_Weapon_Level(CWeapon::WEAPON::LONGSWORD),MAX_WEP_LEVEL );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.9f, 0.9f, 1.0f);
					
					if ( theLevels->CheckWeaponUnlock( CWeapon::LONGSWORD ) )
					{
						glColor3f( 0.0f, 1.0f, 0.0f );
						freetype::print( our_font, 568, 230, "UNLOCKED" );
						theGameMenu->CheckWeapon = true;
					}
					else
					{
						glColor3f( 1.0f, 0.0f, 0.0f );
						freetype::print( our_font, 590, 230, "LOCKED" );
						theGameMenu->CheckWeapon = false;
					}
				glPopMatrix();

				glPopMatrix();
			}
			else if(theGameMenu->LobbyButtonType == CGameMenu::DUAL_SWORD_SHOP_BUTTON)
			{
				glPushMatrix();
					glScalef(3.0f, 3.0f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 330, 220, "%d/%d", theLevels->GetInventory()->CheckQuantity( MATREQ_ITEM_DS ), MATREQ_DS );
				glPopMatrix();

				glPushMatrix();
				glTranslatef(0.0f, 0.0f, 0.0f);

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(1.0f, 1.0f, 0.0f);
					freetype::print(our_font, 560, 390, "Dual Sword" );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 560, 360, "Damage: \nAttack speed: \nWeapon Level: " );
				glPopMatrix();
	
				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(1.0f, 0.0f, 0.0f);
					freetype::print(our_font, 640, 360, "Low" );
				glPopMatrix();
	
				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 0.0f);
					freetype::print(our_font, 680, 325, "Fast " );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 1.0f);
					freetype::print(our_font, 680, 290, "%d/%d", theLevels->GetPlayer()->Check_Weapon_Level(CWeapon::WEAPON::DUALSWORD),MAX_WEP_LEVEL );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.9f, 0.9f, 1.0f);
					
					if ( theLevels->CheckWeaponUnlock( CWeapon::DUALSWORD ) )
					{
						glColor3f( 0.0f, 1.0f, 0.0f );
						freetype::print( our_font, 568, 230, "UNLOCKED" );
						theGameMenu->CheckWeapon = true;

					}
					else
					{
						glColor3f( 1.0f, 0.0f, 0.0f );
						freetype::print( our_font, 590, 230, "LOCKED" );
						theGameMenu->CheckWeapon = false;
					}
				glPopMatrix();

				glPopMatrix();
			}
			else if(theGameMenu->LobbyButtonType == CGameMenu::POTION_BUTTON)
			{
				//glPushMatrix();
				//	glScalef(3.0f, 3.0f, 1.0f);
				//	glColor3f(0.0f, 0.0f, 1.0f);
				//	freetype::print(our_font, 330, 220, "%d/%d", theLevels->GetInventory()->CheckQuantity( MATREQ_ITEM_POT ), MATREQ_POT );
				//glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(1.0f, 1.0f, 0.0f);
					freetype::print(our_font, 560, 390, "Potion" );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 560, 360, "Heal life point by: " );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);
					glColor3f(0.0f, 1.0f, 0.0f);
					freetype::print(our_font, 700, 360, "5" );
				glPopMatrix();

				glPushMatrix();
					glScalef(0.5f, 0.5f, 1.0f);

					glColor3f(0.0f, 0.0f, 1.0f);
					freetype::print(our_font, 560, 325, "Current amount: ");

					glColor3f(0.0f, 1.0f, 1.0f);
					freetype::print(our_font, 700, 325, "%d", theLevels->GetInventory()->CheckQuantity(Item::no::POTIONS));
				glPopMatrix();
			}
		}
	}

	theCamera->SetHUD( false );

	// Flush off any entity which is not drawn yet, so that we maintain the frame rate.
	glFlush();

	// swapping the buffers causes the rendering above to be shown
	glutSwapBuffers();
}

void myApplication::changeSize(int w, int h) {

	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;

	float ratio = (float) (1.0f* w / h);

	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45,ratio,1,1000);
	glMatrixMode(GL_MODELVIEW);
}

void myApplication::SpecialInputKeyDown(int key, int x, int y) {
	myKeys[key] = true;
}

void myApplication::SpecialInputKeyUp(int key, int x, int y) {
	myKeys[key] = false;
}

void myApplication::KeyboardDown(unsigned char key, int x, int y){
	myKeys[key] = true;
}

void myApplication::KeyboardUp(unsigned char key, int x, int y){
	myKeys[key] = false;
}

void myApplication::MouseMove (int x, int y) {
	mouseInfo->lastX = x;
	mouseInfo->lastY = y;
}

void myApplication::MouseClick(int button, int state, int x, int y) {
	switch (button) {

		case GLUT_LEFT_BUTTON:
			mouseInfo->mLButtonUp = state;
			mouseInfo->lastX = x;
			mouseInfo->lastY = y;

			if (!mouseInfo->mLButtonUp) {
				mouseInfo->lastX = x;
				mouseInfo->lastY = y;
			}

			break;

		case GLUT_RIGHT_BUTTON:
			mouseInfo->mRButtonUp = state;
			break;

		case GLUT_MIDDLE_BUTTON:
			break;
	}
}

bool myApplication::LoadAll(void)
{
	// done loading
	if ( track_loading_done ) return true;

	if ( track_loading == 0 )
	{
		our_font.init("Test.ttf", 22);
	}
	else if ( track_loading == 1 )
	{
		//Game Menu
		theGameMenu = new CGameMenu();
	}
	else if ( track_loading == 2 )
	{
		//Sound
		theSound = new Sound();
		theSound->Init();
	}
	else if ( track_loading == 3 )
	{
		theLevels = new LevelSwitcher;
		theLevels->InitLevelSwitcher(mouseInfo, theGameMenu, theSound);
	}
	// done loading
	else
	{
		track_loading_done = true;

		theSound->Play(Sound::BACKGROUND,true);
	}

	// load next
	track_loading ++;

	return track_loading_done;
}

bool myApplication::Init(void)
{
	// Set camera position
	theCamera = new Camera( Camera::LAND_CAM );
	theCamera->SetPosition( 0.0, 2.0, -5.0 );
	theCamera->SetDirection( 0.0, 0.0, 1.0 );

	mouseInfo = new MouseInfo;

	// load splash
	LoadTGA(&splash, "images/background.tga");
	LoadTGA(&loading, "images/Loading.tga");

	//  The number of frames
	frameCount = 0;
	//  Number of frames per second
	fps = 0;
	//  currentTime - previousTime is the time elapsed
	//  between every call of the Idle function
	currentTime = 0, previousTime = 0;

	// realtime loop control
	theRTL = new CRealTimeLoop( 60.0f );

	for(int i=0; i<255; i++){
		myKeys[i] = false;
	}
	angle = 0.0f;
	mouseInfo->lastX = 800 >> 1;
	mouseInfo->lastY = 600 >> 1;

	return true;
}

//-------------------------------------------------------------------------
// Calculates the frames per second
//-------------------------------------------------------------------------
void myApplication::calculateFPS()
{
	//  Increase frame count
	frameCount++;

	//  Get the number of milliseconds since glutInit called
	//  (or first call to glutGet(GLUT ELAPSED TIME)).
	currentTime = glutGet(GLUT_ELAPSED_TIME);

	//  Calculate time passed
	int timeInterval = currentTime - previousTime;

	if(timeInterval > 1000)
	{
		//  calculate the number of frames per second
		fps = frameCount / (timeInterval / 1000.0f);

		//  Set time
		previousTime = currentTime;

		//  Reset frame count
		frameCount = 0;
	}
}

//-------------------------------------------------------------------------
//  Draw FPS
//-------------------------------------------------------------------------
void myApplication::drawFPS(freetype::font_data font)
{
	glPushMatrix();
		//  Load the identity matrix so that FPS string being drawn
		//  won't get animates
		glLoadIdentity ();
		glPushAttrib(GL_DEPTH_TEST);
		glDisable(GL_DEPTH_TEST);

			//  Print the FPS to the window
			if (fps > 28.30f)
				glColor3f( 0.0f, 1.0f, 1.0f);
			else if (fps > 28.0f)
				glColor3f( 0.0f, 1.0f, 0.0f);
			else 
				glColor3f( 1.0f, 0.0f, 0.0f);
			print (font, 300.0, 200.0, "FPS: %4.2f", fps);

		glPopAttrib();
		glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}